package com.awesome.spring.mq;

import com.alibaba.fastjson.JSON;
import com.awesome.spring.bean.Order;
import com.awesome.spring.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @program: awesome-spring
 * @description: 订单消费
 * @author: Alex Wu
 * @createDate: 2025-02-13 14:38
 **/
@Slf4j
@RocketMQMessageListener(topic = "order-fail-topic", consumerGroup = "order-fail-consumer-group")
@Service
public class OrderConsumer implements RocketMQListener<String> {

    @Autowired
    private OrderService orderService;

    @Override
    public void onMessage(String message) {
        // 上游实在无法处理的信息
        Order order = JSON.parseObject(message, Order.class);
        if(order.getStatus() == 0){
            log.info("需要删除消费订单消息: {}", order);
            orderService.removeById(order.getId());
        }else {
            log.info("上游处理成功的订单，也可本地存储记录: {}", order);
        }
    }
}
